/*
 * Chastoslov
 * Copyright (C) 2009 Alexandr Stroghov
 * 
 * stroghov@gmail.com
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms of the 
 * GNU General Public License, version 2, as published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program; if 
 * not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
 * MA 02111-1307 USA
*/
package world.ah.wf.guiclient;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.Dimension;
import javax.swing.JDialog;
import javax.swing.JScrollBar;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.event.ComponentEvent;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;

public class DialogError extends JDialog {
  //
  private final int P_DETAILS_MIN_SIZE = 10;
  private String showDetailsLabel = "Show details";
  private String hideDetailsLabel = "Hide details";
  // 
  public void setErrorText(String errorText) {
    taErrorText.setText(errorText);
  } 
  public void setDetails(String details) {
    taDetails.setText(details);
  } 
  // 
  private JButton bDetails = new JButton();
  private JButton bOK = new JButton();
  private JScrollPane spDetails = new JScrollPane();
  private JTextArea taDetails = new JTextArea();
  private JScrollPane spErrorText = new JScrollPane();
  private JTextArea taErrorText = new JTextArea();
  private JPanel pNorth = new JPanel();
  private BorderLayout borderLayout1 = new BorderLayout();
  private JPanel pCenter = new JPanel();
  private GridBagLayout gridBagLayout1 = new GridBagLayout();
  private GridBagLayout gridBagLayout2 = new GridBagLayout();

  public DialogError() {this((Frame)null, "", false);}

  /**
   * 
   * @param parent
   * @param title
   * @param modal
   */
  public DialogError(Frame parent, String title, boolean modal) {
    super(parent, title, modal);
    myInit();
  }
  public DialogError(Dialog parent, String title, boolean modal) {
    super(parent, title, modal);  
    myInit();
  }
  //
  private void myInit() {
    try {jbInit();} catch(Exception e) {e.printStackTrace();}
    //
    taErrorText.setFont(null);
    taDetails.setFont(null);
    General.centerFrame(this);
    
    // Устанавливаем размер, чтобы он потом был увеличен
    Dimension sz = this.getSize();
    sz.setSize(this.getWidth(), spErrorText.getWidth()); 
    this.setSize(sz);
    //
    setLang();
    
  }
  //
  private void jbInit() throws Exception {
    this.setSize(new Dimension(402, 259));
    this.getContentPane().setLayout(borderLayout1);
    this.setTitle("Error!");
    this.addComponentListener(new java.awt.event.ComponentAdapter()
      {
        public void componentShown(ComponentEvent e)
        {
          this_componentShown(e);
        }

        public void componentResized(ComponentEvent e)
        {
          this_componentResized(e);
        }
      });
    bDetails.setText(showDetailsLabel);
    bDetails.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent e)
        {
          bDetails_actionPerformed(e);
        }
      });
    bOK.setText("OK");
    bOK.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent e)
        {
          bOK_actionPerformed(e);
        }
      });
        taDetails.setEditable(false);
    taDetails.setWrapStyleWord(true);
    taDetails.setLineWrap(true);
    spErrorText.setPreferredSize(new Dimension(376, 90));
    taErrorText.setEditable(false);
    taErrorText.setWrapStyleWord(true);
    taErrorText.setLineWrap(true);
    pNorth.setLayout(gridBagLayout2);
    pCenter.setLayout(gridBagLayout1);
    spDetails.getViewport();
    spErrorText.getViewport();
    spErrorText.getViewport().add(taErrorText, null);
    pNorth.add(spErrorText, new GridBagConstraints(0, 0, 2, 1, 1.0, 1.0, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(10, 10, 10, 10), 0, 6));
    pNorth.add(bOK, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 10, 10, 10), 0, 0));
    pNorth.add(bDetails, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 10, 10, 10), 0, 0));
    this.getContentPane().add(pNorth, BorderLayout.NORTH);
    spDetails.getViewport().add(taDetails, null);
    pCenter.add(spDetails, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 10, 10, 10), 0, 0));
    this.getContentPane().add(pCenter, BorderLayout.CENTER);
  }
  /** Устанавливает язык интерфейса */
  public void setLang() {
    String s;
    s = General.getLangItem("Error"); if(s.length() > 0) this.setTitle(s + "!");
    showDetailsLabel = General.getLangItem("ShowDetails", "Show details");
    hideDetailsLabel = General.getLangItem("HideDetails", "Hide details");
    makeBDetailsText();
    bOK.setText(General.getLangItem("OK", "OK"));
  }
  
  private void bOK_actionPerformed(ActionEvent e) {
    this.setVisible(false);
  }
  //
  private void changeShowDetails() { 
    int minh = getMunumumHeight();
    Dimension sz = this.getSize();
    if(spDetails.getHeight() > P_DETAILS_MIN_SIZE) sz.setSize(this.getWidth(), minh);
    else sz.setSize(this.getWidth(), minh + 150);
    this.setSize(sz);
  }

  private void bDetails_actionPerformed(ActionEvent e) {
    changeShowDetails();
  }
  private int getMunumumHeight() {
    
    int gap = this.getHeight() - this.getContentPane().getHeight();
    //pCenter.getHeight();
    int minh = gap + pNorth.getHeight();
    
    //System.out.println("MH = " + Integer.toString(minh));
    return minh;
  }
  //
  private void makeBDetailsText() {
    if(spDetails.getHeight() > P_DETAILS_MIN_SIZE) bDetails.setText(hideDetailsLabel);
    else bDetails.setText(showDetailsLabel);
  }

  private void this_componentResized(ComponentEvent e) {
    General.controlMinimumSize(this, 300, getMunumumHeight());
    makeBDetailsText();
  }

  private void this_componentShown(ComponentEvent e) {
    JScrollBar sb;
    taErrorText.setSelectionStart(0);
    taErrorText.setSelectionEnd(0);
    sb = spErrorText.getVerticalScrollBar();
    sb.setValue(sb.getMinimum());
    taDetails.setSelectionStart(0);
    taDetails.setSelectionEnd(0);
    sb = spDetails.getVerticalScrollBar();
    sb.setValue(sb.getMinimum());
  }


}